package uk.ac.ncl.carrent;

import java.util.*;

/**
 * A car registration number has two components
 * a single letter followed by a four digit number.
 * @author Shiwei He
 */

public class RegistrationNumber {

	private static final Map<String, RegistrationNumber> reg = new HashMap<String, RegistrationNumber>();
	private final String letter;
	private final int number;

	public RegistrationNumber(String letter, int number){
		if (letter.length() == 0)
            throw new IllegalArgumentException("Empty Letter");
		if(number == 0)
			throw new IllegalArgumentException("Empty Number");
		this.letter = letter;
		this.number = number;
	}

	public final static RegistrationNumber getInstance(String rg){
		RegistrationNumber regNo = reg.get(rg);
		if(regNo == null){
			regNo = new RegistrationNumber(rg.substring(0, 1), Integer.parseInt(rg.substring(1, 4)));
			return regNo;
		}
		return regNo;
	}
	/**
	 * get the letter part of the registration number
	 * @return String the letter of the registration number
	 */
	public String getLetter(){
		return letter;
	}
	
	/**
	 * get the number part of the registration number
	 * @return int the number of the registration number
	 */
	public int getNumber(){
		return number;
	}
	
	/**
	 * the String representation of the registration number
	 * @return String the registration number
	 */
	public String toString(){
		return letter + number;
	}

	public static RegistrationNumber valueOf(String reg) {
        final String[] parts = new String[4];
        parts[0] = reg.substring(0, 1);
        parts[1] = reg.substring(1, 4); 
        return new RegistrationNumber(parts[0],Integer.parseInt(parts[1]));
	}
	
	
}
